﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class ServiceCategories
    {

        public ServiceCategories() { }

        #region [Get all categories]
        /// <summary>
        /// Get all categories
        /// </summary>
        /// <returns>
        /// <para>Return value of Categories of Service Package</para>
        /// <para>Null  : error, watch log file to know issuse</para>
        /// </returns>
        public List<PC_Categories_getAllResult> getAll()
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_Categories_getAll().ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get all Category by Service Package]
        /// <summary>
        /// Get all Category by Service Package
        /// </summary>
        /// <param name="packageID">ID of Service Package</param>
        /// <param name="getActive">
        /// <para>0     : false</para>
        /// <para>1     : true</para>
        /// <para>-1    : get all</para>
        /// </param>
        /// <returns>
        /// <para>Return all Service Categories in Package</para>
        /// <para>null  : error</para>
        /// </returns>
        public List<PC_Categories_GetAllByPackageIDResult> getAllByPackageID(string packageID, string getActive)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_Categories_GetAllByPackageID(Int32.Parse(packageID), Int32.Parse(getActive)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert a new Service Category]
        /// <summary>
        /// Insert a new Service Category
        /// </summary>
        /// <param name="name">Name of Service Category</param>
        /// <param name="description">A short description about Service Category</param>
        /// <param name="fee">Total money for a Service Category</param>
        /// <param name="sPId">ID of Service Package</param>
        /// <param name="active">Visible/Hidden Service Category and children</param>
        /// <returns>
        /// <para>0    : successfully</para>
        /// <para>-99  : categories insert error</para>
        /// </returns>
        public int add(string name, string description, string fee, string sPId, bool active)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_Categories_Ins(name, description, Double.Parse(fee), Int32.Parse(sPId), active);
                return isInsert;
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check insert is valid or not]
        /*Return:
         * 1    : successfully
         * 4    : Package id is null or empty
         * 5    : Package id is not valid
         * 20   : Fee is not valid
         * 40   : description is greater than 2000 characters
         */
        public int addValid(string name, string description, string fee, string sPId)
        {
            if (String.IsNullOrEmpty(sPId))
            {
                return 4;
            }

            Regex rsPId = new Regex("^[0-9]{1,}$");
            Match msPId = rsPId.Match(sPId);
            if (!msPId.Success)
            {
                return 5;
            }
            else if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 50)
            {
                return 11;
            }

            Regex rFee = new Regex("^[0-9]{1,}.{0,1}[0-9]{1,}$");
            Match mFee = rFee.Match(fee);
            if (!mFee.Success)
            {
                return 20;
            }
            else if (description.Length > 2000)
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Edit an Service Category]
        /// <summary>
        /// Edit an Service Category
        /// </summary>
        /// <param name="id">ID of Service Category</param>
        /// <param name="name">Name of Service Category</param>
        /// <param name="description">A short description about Service Category</param>
        /// <param name="fee">Total money for a Service Category</param>
        /// <param name="sPId">ID of Service Package</param>
        /// <param name="active">Visible/Hidden Service Category and children</param>
        /// <returns>
        /// <para>0    : successfully</para>
        /// <para>99  : edit insert error</para>
        /// </returns>
        public int edit(string id, string name, string description, string fee, string sPId, bool active)
        {
            HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
            var isUpdate = db.PC_Categories_Upd(Int32.Parse(id), name, description, Double.Parse(fee), Int32.Parse(sPId), active);
            return isUpdate;
        }
        #endregion

        #region [Check edit is valid or not]
        /// <summary>
        /// Check edit is valid or not
        /// </summary>
        /// <param name="id">ID of Service Category</param>
        /// <param name="name">Name of Service Category</param>
        /// <param name="description">A short description about Service Category</param>
        /// <param name="fee">Total money for a Service Category</param>
        /// <param name="sPId">ID of Service Package</param>
        /// <returns>
        /// <para>1    : successfully</para>
        /// <para>2    : id is null or empty</para>
        /// <para>3    : id is not valid</para>
        /// <para>10    : Name is null or empty</para>
        /// <para>11    : Name is greater than 50 characters</para>
        /// <para>20   : Fee is not valid</para>
        /// <para>40   : description is greater than 2000 characters</para>
        /// </returns>
        public int editValid(string id, string name, string description, string fee, string sPId)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }

            Regex rId = new Regex("^[0-9]{1,}$");
            Match mId = rId.Match(id);
            if (!mId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(sPId))
            {
                return 4;
            }

            Regex rsPId = new Regex("^[0-9]{1,}$");
            Match msPId = rsPId.Match(sPId);
            if (!msPId.Success)
            {
                return 5;
            }
            else if (String.IsNullOrEmpty(name))
            {
                return 10;
            }
            else if (name.Length > 50)
            {
                return 11;
            }
            
            Regex rFee = new Regex("^[0-9]{1,}.{0,1}[0-9]{1,}$");
            Match mFee = rFee.Match(fee);
            if (!mFee.Success)
            {
                return 20;
            }
            else if (description.Length > 2000)
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Service Category]
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isDelete = db.PC_Categories_Del(Int32.Parse(id));
                return isDelete;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}